package com.houserent.view;

import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;

import java.util.Objects;

/**
 * 1、显示界面
 * 2、接受用户输入
 * 3、调用HouseService完成对房屋信息的各种操作
 */

public class HouseView {



    private boolean loop = true; // 控制显示菜单
    private char key = ' '; // 接受用户输入
    private HouseService houseService = new HouseService(10); // 设置房屋列表的大小为10

    // 修改房屋信息
    public void modifyHouse() {
        System.out.println("-------------------修改房屋信息-------------------");
        System.out.print("请输入你要修改的房屋id（-1：退出）：");
        int modifyId = Utility.readInt();
        House tempObject = houseService.returnObjectById(modifyId);
        System.out.print("姓名（" + tempObject.getName() + "）：");
        String name = Utility.readString(8, tempObject.getName());
        tempObject.setName(name);
        System.out.print("电话（" + tempObject.getPhone() + "）：");
        String phone = Utility.readString(12, tempObject.getPhone());
        tempObject.setPhone(phone);
        System.out.print("地址（" + tempObject.getAddress() + "）：");
        String address = Utility.readString(16, tempObject.getAddress());
        tempObject.setAddress(address);
        System.out.print("租金（" + tempObject.getRent() + "）：");
        int rent = Utility.readInt(tempObject.getRent());
        tempObject.setRent(rent);
        System.out.print("状态（" + tempObject.getState() + "）：");
        String state = Utility.readString(3, tempObject.getState());
        tempObject.setState(state);
        System.out.println("-------------------修改信息成功-------------------");
    }

    // 根据房屋位置查找
    public void searchHouseByAddress() {
        System.out.println("-------------------查找房屋-------------------");
        System.out.print("请输入你要查找的区域（-1：退出）：");
        String targetAddress = Utility.readString(16);

        if (targetAddress.equals("-1")) {
            System.out.println("-------------------放弃查找房屋信息-------------------");
            return;
        }
        if (houseService.search(targetAddress)) {
            System.out.println("-------------------查找结束-------------------");
        } else {
            System.out.println("-------------------房屋信息不存在，查找失败-------------------");
        }
    }

    // 退出确认
    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

    // 删除房屋信息
    public void delHouse() {
        System.out.println("-------------------删除房屋-------------------");
        System.out.print("请输入待删除房屋的编号（-1：退出）：");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("-------------------放弃删除房屋信息-------------------");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("-------------------删除房屋信息成功-------------------");
            } else {
                System.out.println("-------------------房屋信息不存在，删除失败-------------------");
            }
        } else {
            System.out.println("-------------------放弃删除房屋信息-------------------");
        }
    }

    // 编写addHouse() 接受输入，创建House对象，调用addHouse方法
    public void addHouse() {
        System.out.println("-------------------添加房屋-------------------");
        System.out.print("姓名：");
        String name = Utility.readString(8);
        System.out.print("电话：");
        String phone = Utility.readString(12);
        System.out.print("地址：");
        String address = Utility.readString(16);
        System.out.print("租金：");
        int rent = Utility.readInt();
        System.out.print("状态：");
        String state = Utility.readString(3);
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("-------------------添加房屋成功-------------------");
        } else {
            System.out.println("-------------------添加房屋失败-------------------");
        }
    }

    // 编写listHouses()显示房屋列表
    public void listHouses() {
        System.out.println("-------------------房屋列表-------------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态（未出租/已出租）");
        House[] houses = houseService.list(); //得到所有房屋信息
        for (House house : houses) {
            if (house == null) {break;}
            System.out.println(house);
        }
        System.out.println("-------------------列表结束-------------------");
    }

    // 显示主菜单
    public void mainMenu() {
        do {
            System.out.println("-------------------房屋出租系统菜单-------------------");
            System.out.println("\t\t\t\t1 新增房源");
            System.out.println("\t\t\t\t2 查找房屋");
            System.out.println("\t\t\t\t3 删除房屋信息");
            System.out.println("\t\t\t\t4 修改房屋信息");
            System.out.println("\t\t\t\t5 房屋列表");
            System.out.println("\t\t\t\t6 退出");
            System.out.print("请输入你的选择（1-6）：");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    this.addHouse();
                    break;
                case '2':
                    this.searchHouseByAddress();
                    break;
                case '3':
                    this.delHouse();
                    break;
                case '4':
                    this.modifyHouse();
                    break;
                case '5':
                    this.listHouses();
                    break;
                case '6':
                    this.exit();
                    break;
                default:
                    System.out.println("asd");
            }
        } while (loop);
    }
}
